package com.bxait.toymail.ware.controller;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;


import com.bxait.com.utils.PageUtils;
import com.bxait.com.utils.R;
import com.bxait.toymail.ware.feign.ProductFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bxait.toymail.ware.entity.PurchaseDetailEntity;
import com.bxait.toymail.ware.service.PurchaseDetailService;

import javax.annotation.Resource;


/**
 * import org.apache.shiro.authz.annotation.RequiresPermissions;
 * 
 *
 * @author chen
 * @email 2277411582@qq.com
 * @date 2023-03-13 22:41:32
 */
@RestController
@RequestMapping("ware/purchasedetail")
public class PurchaseDetailController {
    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Resource
    ProductFeignService productFeignService;

    /**
     * @param params
     * @==============History===============<br/>
     * @Description // TODO  获取采购需求
     * @Date 2023/3/25 2023/3/25
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return [java.util.Map<java.lang.String,java.lang.Object>]
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseDetailService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		PurchaseDetailEntity purchaseDetail = purchaseDetailService.getById(id);

        return R.ok().put("purchaseDetail", purchaseDetail);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PurchaseDetailEntity purchaseDetail){
        Long skuId = purchaseDetail.getSkuId();
        R info = productFeignService.info(skuId);
        if(info.getCode() == 0){
            Map<String, Object> data = (Map<String, Object>)info.get("data");
            Double price = (Double) data.get("price");

            BigDecimal priceNew = new BigDecimal(price).multiply(new BigDecimal(purchaseDetail.getSkuNum()));
            purchaseDetail.setSkuPrice(priceNew);
        }

        purchaseDetailService.save(purchaseDetail);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PurchaseDetailEntity purchaseDetail){
		purchaseDetailService.updateById(purchaseDetail);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		purchaseDetailService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
